/*
 This file 'DoubleSlider' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2017-02-08 15:48
 */

define(function (require, exports, module) {

	require("Firebird.ui.Slider");

	/**
	 * @class Firebird.ui.DoubleSlider
	 *
	 * @example
	 */
	Firebird.define("Firebird.ui.DoubleSlider", {
		extend: "Firebird.ui.Slider",
		statics: {
			_uuid: 1,
			uuid: function () {
				return this._uuid++;
			},
			attrs: function ($component) {
				var attrs = {};
				attrs = $.extend({}, {
					renderTo: $component.get(0),
					instanceId: $component.attr("fb-instance") || $component.attr("name") || $component.attr("id") || ("doubleslider_" + Firebird.ui.DoubleSlider.uuid()),

					hidden: Firebird.ui.Component.eval($component.attr("fb-hidden")),


					prefix: $component.attr("fb-prefix") || "",
					suffix: $component.attr("fb-suffix") || "",
					//animate
					//classes
					disabled: Firebird.ui.Component.eval($component.attr("fb-disabled") || $component.prop("disabled") || false),
					max: Firebird.ui.Component.eval($component.attr("fb-max") || 100),
					min: Firebird.ui.Component.eval($component.attr("fb-min") || 1),
					orientation: $component.attr("fb-orientation") || "horizontal",
					range: Firebird.ui.Component.eval($component.attr("fb-range") || true),
					step: Firebird.ui.Component.eval($component.attr("fb-step") || 1),
					value: (function () {
						var value = $component.attr("fb-value") || $component.val() || [0, 0];
						// [1,2] 1.2 [1,2 1,2]
						if (typeof value === "string") {
							value = value.replace(/[|]/g, "");
							value = Firebird.ui.Component.eval("[" + value + "]");
						}

						return value;
					})(),

					css: $component.attr("fb-css") || "",

					/**
					 * {0} {1}
					 */
					label: $component.attr("fb-label") || "",

					format: ""



				});
				return attrs;
			}
		},
		config: {
			renderTo: null,
			instanceId: null,

			width: "100%",

			disabled: false,
			max: 100,
			min: 0,
			/**
			 * Determines whether the slider handles move horizontally (min on left, max on right) or vertically (min on bottom, max on top). Possible values: "horizontal", "vertical".
			 */
			orientation: "horizontal",
			range: true,
			step: 1,
			value: null,
			label: "",

			prefix: "",
			suffix: "",

			format: "",

			css: "",
			/**
			 * 模式
			 * normal 普通
			 * double 双数时，value 为的一个数组，其长度为2
			 */
			mode: "normal"
		},


		/**
		 * wrapCls
		 */
		wrapCls: "fb-ui-slider",

		instance: null,

		_init: function () {
			var me = this;
			var uuid = me.uuid = Firebird.lang.Uuid.generate();

			var renderTo = me.renderTo = $(me.getRenderTo());
			me.instance = renderTo.data({
				instance: me,
				xtype: "slider"
			}).css("display", "none");

			me.render();

			me.wrapper = $("#" + uuid + "_wrapper");
			me.slider = $("#" + uuid + "_slider").attr("style", me.getCss());
			me.handler = $("#" + uuid + "_handler");

			me.wrapper.append(me.instance);

			me.options = $.extend(true, {}, {

				orientation: me.getOrientation(),


				range: true,
				step: me.getStep(),
				max: me.getMax(),
				min: me.getMin(),


				change: function (e, ui) {
					var value = ui.values;
					me._setValue(value);
					me.proxy("onChange").apply(me, [e, ui]);
				},
				start: function (e, ui) {
					var value = ui.values;
					me._setValue(value);
					me.proxy("onStart").apply(me, [e, ui]);
				},
				stop: function (e, ui) {
					var value = ui.values;
					me._setValue(value);
					me.proxy("onStop").apply(me, [e, ui]);
				},
				slide: function (e, ui) {
					var value = ui.values;
					me._setValue(value);
					me.proxy("onSlide").apply(me, [e, ui]);
				},
				create: function (e, ui) {
					me.proxy("onCreate").apply(me, [e, ui]);
				},
				values: Firebird.ui.Component.eval("[" + me.getValue() + "]")
			});

			me.slider
				.slider(me.options)
				.slider("pips", {

					// formatLabel: me.getFormat(),
					// rest: "label",
					// labels: [],
					// prefix: me.getPrefix(),
					// suffix: me.getSuffix()
				})                                                        
				.slider("float")
				// and whenever the slider changes, lets echo out the month
				.on("slidechange", function (e, ui) {

					// var value = ui.values;
					// me._setValue(value);
					me.proxy("onSlideChange").apply(me, [e, ui]);
					//console.log("slidechange:", ui.value)
					//$("#labels-months-output").text("You selected " + months[ui.value] + " (" + ui.value + ")");

				});


			me.setDisabled(me.getDisabled());
			me.setValue(me.getValue());
		},

		render: function () {

			var me = this;
			me.instance.after("" +
				"<div id='" + me.uuid + "_wrapper' class='" + me.wrapCls + "-wrapper'>" +
				"	<div  id='" + me.uuid + "_slider'  class='" + me.wrapCls + "-slider'>" +
				"	</div>" +
				"</div>");

		},

		applyMin: function (min) {
			var me = this;

			if (this.instance && min != null && typeof (min) != "undefined") {
				//this._setValue(val);
				this.option("min", min);
			}
			return min;
		},

		applyMax: function (max) {
			var me = this;

			if (this.instance && max != null && typeof (max) != "undefined") {
				// this._setValue(val);
				this.option("max", max);
			}
			return max;
		},


		applyValue: function (val) {
			var me = this;

			if (this.instance && val != null && typeof (val) != "undefined") {
				this._setValue(val);
				this.option("values", Firebird.ui.Component.eval("[" + me.getValue() + "]"));
			}
			return val;
		},

		_setValue: function (val) {
			if (this.instance) {
				val = typeof val === "string" ? val : val.join(",");
				this.value = Firebird.ui.Component.eval("[" + val + "]");
				this.instance.val(val);
			}
		},

		getValue: function () {
			return this.value.join(",")
		},

		updateValue: function (val) {
			if (this.instance && val != null && typeof (val) != "undefined") {
				this._setValue(val);
				this.option("values", Firebird.ui.Component.eval("[" + this.getValue() + "]"));
			}
			return val;
		}
	});

	module.exports = Firebird.ui.DoubleSlider;

});